package com.zxbit.uangstore.main;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.facebook.accountkit.AccountKitLoginResult;
import com.facebook.accountkit.ui.AccountKitActivity;
import com.facebook.accountkit.ui.AccountKitConfiguration;
import com.facebook.accountkit.ui.LoginType;
import com.zxbit.uangstore.BuildConfig;
import com.zxbit.uangstore.R;
import com.zxbit.uangstore.base.BaseActivity;
import com.zxbit.uangstore.business.RouterMap;
import com.zxbit.uangstore.cache.UserInfo;
import com.zxbit.uangstore.constant.Constant;
import com.zxbit.uangstore.domain.LoginResponse;
import com.zxbit.uangstore.mvpview.LoginMvpView;
import com.zxbit.uangstore.presenter.LoginPresenter;
import com.zxbit.uangstore.utils.StringUtils;
import com.zxbit.uangstore.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import crossoverone.statuslib.StatusUtil;

@SuppressLint("CheckResult")
@Route(name = "选择登录方式页面", path = RouterMap.LOGIN_METNOD_PAGE)
public class LoginMethodActivity extends BaseActivity implements LoginMvpView {
    public static int APP_REQUEST_CODE = 99;

    @BindView(R.id.display_show_text)
    TextView display_show_text; // 调试用

    private LoginPresenter presenter;

    @Override
    protected void initViews() {
        ButterKnife.bind(this, this);
    }

    @Override
    protected void onUIReady() {
        presenter = new LoginPresenter(this);
        if (BuildConfig.DEBUG) {
            display_show_text.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void setSystemInvadeBlack() {
        StatusUtil.setUseStatusBarColor(this, Color.parseColor("#00000000"));
        // 第二个参数是是否沉浸,第三个参数是状态栏字体是否为黑色。
        StatusUtil.setSystemStatus(this, true, false);
    }

    @Override
    protected int getContentResId() {
        return R.layout.layout_of_login_method;
    }

    @Override
    protected int getResId() {
        return 0;
    }

    @OnClick({R.id.tv_login_by_facebook, R.id.iv_facebook_icon, R.id.display_show_text})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_login_by_facebook:
            case R.id.iv_facebook_icon:
               phoneLogin();
                break;
            case R.id.display_show_text:
                if (display_show_text.getText() == null) return;
                presenter.doUploadAuthCode(display_show_text.getText().toString());
                showLoadingView();
                break;
        }
    }

    private void phoneLogin() {
        Intent intent = new Intent(LoginMethodActivity.this, AccountKitActivity.class);
        AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder =
                new AccountKitConfiguration.AccountKitConfigurationBuilder(
                        LoginType.PHONE,
                        AccountKitActivity.ResponseType.CODE); // or .ResponseType.TOKEN

        intent.putExtra(
                AccountKitActivity.ACCOUNT_KIT_ACTIVITY_CONFIGURATION,
                configurationBuilder.build());
        startActivityForResult(intent, APP_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == APP_REQUEST_CODE) {
            AccountKitLoginResult loginResult = data.getParcelableExtra(AccountKitLoginResult.RESULT_KEY);
            String toastMessage;
            if (loginResult.getError() != null) {
                toastMessage = loginResult.getError().getErrorType().getMessage();
            } else if (loginResult.wasCancelled()) {
                toastMessage = getStringResource(R.string.login_page_login_cancelled);
            } else {
                toastMessage = getStringResource(R.string.login_page_login_loading);
                display_show_text.setText(loginResult.getAuthorizationCode());
                presenter.doUploadAuthCode(display_show_text.getText().toString());
                showLoadingView();
            }

            // Surface the result to your user in an appropriate way.
            ToastUtils.showShort(LoginMethodActivity.this, toastMessage);
        }
    }

    @Override
    public void onGetDataNext(LoginResponse loginResponse) {
        dismissLoadingView();
        if (loginResponse.getStatus() == Constant.HTTP_OK) {
            String token = loginResponse.getData().getToken();

            if (StringUtils.isEmpty(token)) {
                ToastUtils.showShort(LoginMethodActivity.this, getStringResource(R.string.token_is_null));
                return;
            }
            UserInfo.setToken(token);
            ToastUtils.showShort(LoginMethodActivity.this, getStringResource(R.string.login_page_login_success));
//            EventBus.getDefault().post(LoginEvent.LOGIN_SUCCESS);   // 通知登陆成功
            ARouter.getInstance().build(RouterMap.MAIN_ACTIVITY_PAGE).navigation();
        } else {
            String toastStr = getStringResource(R.string.login_failed_retry);
            ToastUtils.showShort(LoginMethodActivity.this, toastStr);
        }
    }

    @Override
    public void onGetDataError(Throwable throwable) {
        dismissLoadingView();
    }
}
